home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48_1 / unit.lis < prev    next >
File List  |  1995-03-23  |  7KB  |  304 lines

  1. Article 1850 of comp.sys.handhelds:
  2. Path: en.ecn.purdue.edu!pur-ee!mentor.cc.purdue.edu!purdue!tut.cis.ohio-state.edu!uc!norge.unet.umn.edu!fin
  3. From: fin@norge.unet.umn.edu (Craig A. Finseth)
  4. Newsgroups: comp.sys.handhelds
  5. Subject: HP-48SX Value Unit Program [long]
  6. Message-ID: <1627@uc.msc.umn.edu>
  7. Date: 16 Apr 90 16:56:29 GMT
  8. Sender: news@uc.msc.umn.edu
  9. Organization: Univ Netw Serv, Univ of Minn
  10. Lines: 290
  11.  
  12. The idea for this program came from John Latala
  13. (jrl@images1.Waterloo.NCR.COM) at NCR Canada Ltd, Waterloo, Ontario,
  14. Canada.  It appeared on comp.sys.handhelds on 9 Apr 1990 His program
  15. was written for the HP-28S.  I rewrote the program completely,
  16. changing the interface to follow the lines of APL's base value and
  17. represent operators.  (Archive: hand.8 line 388)
  18.  
  19. BV        Convert a value list into its base value (a decimal integer).
  20. RP        Convert a decimal integer into its representation.
  21. ADDVULIST    Add two value lists.
  22. SUBVULIST    Subtract two value lists.
  23. TOUNIT        Combine a number and a unit (this is what ->UNIT *should* do).
  24. TORATIOS    Convert a unit list into the unit ratios
  25.  
  26. VULTYPE        Return the type of a unit list.
  27. VULVAL        Return the place value of a unit list.
  28.  
  29. ------------------------------------------------------------
  30. Data Types:
  31.  
  32. - All numbers used by this program are integers in real numbers only.
  33. - This program cannot handle non-integral or negative bases.
  34. - All lists supplied to an invocation of these programs must be the
  35. same length.
  36. - As it turns out, the high order entry in a unit list is never used. 
  37. It is merely present so that the second entry is not the high order
  38. entry...
  39.  
  40. Unit Lists can appear in one of four forms:
  41.  
  42. 1) A number.  This means that the values in the value list should be
  43. interpreted as being in the specified base.  This can be thought of as
  44. being the same as the second form, with the number being repeated as
  45. often as required.  Example: 10
  46.  
  47. 2) A value list.  This means that the values in the value list should
  48. be interpreted as specifying a compound base, with the weights of each
  49. position being specified by the corresponding number from this list. 
  50. Example:  { 1 24 60 60 }    
  51.  
  52. 3) A unit list.  This means that the values in the value list should
  53. be interpreted as specifying a compound base, with the weights of each
  54. position being specified by the unit from this list.
  55. Example:  { d h min s }
  56.  
  57. 4) A unit object list.  This means that the values in the value list
  58. should be interpreted as specifying a compound base, with the weights
  59. of each position being specified by the unit object from this list.
  60. The number part of the unit object is ignored.
  61. Example: { 1_d 5_h 2_min 1_s }
  62.  
  63. ------------------------------------------------------------
  64. Detailed Interfaces:
  65.  
  66. BV    Stack Input:    value list
  67.             unit list
  68.     Stack Output:    number
  69.     Calls:        VULTYPE, TOUNIT
  70.  
  71.     Convert the value list into its base value.  Examples:
  72.  
  73.     { 1 2 3 } 10        BV    123
  74.     { 1 2 3 } { 4 5 6 }    BV    45
  75.     { 1 2 3 } { yd ft in }    BV    63_in
  76.  
  77.  
  78. RP    Stack Input:    number
  79.             unit list
  80.     Stack Output:    value list
  81.     Calls:        VULTYPE, VULVAL, TORATIOS
  82.  
  83.     Convert the value into its representation in the specified
  84.     base.  Examples:
  85.  
  86.     123 10        RP    { 1 2 3 }
  87.     45 { 4 5 6 }    RP    { 1 2 3 }
  88.     63_in { yd ft in } RP    { 1 2 3 }
  89.  
  90.  
  91. ADDVULIST Stack Input:    value list 1
  92.             value list 2
  93.             unit list
  94.     Stack Output:    value list sum
  95.     Calls:        BV, RP
  96.  
  97.     Add the two unit lists.  Examples:
  98.  
  99.     { 1 2 3 } { 4 5 6 } 7        ADDVULIST    { 6 1 2 }
  100.  
  101.  
  102. SUBVULIST Stack Input:    value list 1
  103.             value list 2
  104.             unit list
  105.     Stack Output:    value list difference
  106.     Calls:        BV, RP
  107.  
  108.  
  109. TOUNIT    Stack Input:    value
  110.             unit
  111.     Stack Output:    unit object
  112.     Calls:        none
  113.  
  114.     Combines the value and the unit into a unit object.  It works
  115.     even if the unit is not already a unit object...  This (or
  116.     something like this) is what ->UNIT should be doing.
  117.  
  118.  
  119. TORATIOS Stack Input:    unit list type 3 or 4
  120.     Stack Output:    unit list type 2
  121.     Calls:        TOUNIT
  122.  
  123.     Converts the unit list into the ratios implied by the units.  Examples:
  124.  
  125.     { d h min s } TORATIOS    { 1 24 60 60 }
  126.     { yd ft in } TORATIOS    { 1 3 12 }
  127.  
  128. VUTYPE    Stack Input:    unit list
  129.     Stack Output:    internal type:
  130.                 1    number, unit list type 1
  131.                 2    value list, unit list type 2
  132.                 3    unit list, unit list types 3 or 4
  133.                 4    unkown
  134.     Calls:        none
  135.  
  136.  
  137. VUVAL    Stack Input:    unit list type 2
  138.             position
  139.     Stack Output:    place value
  140.     Calls:        BV, RP
  141.  
  142.     Figure the place value for the specified position.  Example:
  143.  
  144.     { 1 24 60 60 } 1 VUVAL    86400
  145.     { 1 24 60 60 } 2 VUVAL    3600
  146.     { 1 24 60 60 } 3 VUVAL    60
  147.     { 1 24 60 60 } 4 VUVAL    1
  148.  
  149.  
  150. Checksum: #41454d
  151. Size: 1404
  152. ------------------------------------------------------------
  153. %%HP: T(3)A(D)F(.);
  154. DIR
  155.   BV
  156.     \<< DUP VULTYPE \->
  157. v b bt
  158.       \<< 0 1 v SIZE
  159.         CASE bt 1
  160. ==
  161.           THEN
  162.             FOR j b
  163. * v j GET +
  164.             NEXT
  165.           END bt 2
  166. ==
  167.           THEN
  168.             FOR j b
  169. j GET * v j GET +
  170.             NEXT
  171.           END bt 3
  172. ==
  173.           THEN
  174.             FOR j v
  175. j GET b j GET
  176. TOUNIT +
  177.             NEXT
  178.           END 3
  179. DROPN v b
  180. "Invalid Base Type"
  181.         END
  182.       \>>
  183.     \>>
  184.   RP
  185.     \<< DUP VULTYPE \->
  186. v b bt
  187.       \<< { }
  188.         CASE bt 1
  189. ==
  190.           THEN
  191.             DO v b
  192. MOD 1 \->LIST SWAP +
  193. v b / IP 'v' STO
  194.             UNTIL v
  195. 1 <
  196.             END
  197.           END bt 2
  198. ==
  199.           THEN 1 b
  200. SIZE
  201.             FOR j b
  202. j VULVAL \-> be
  203.               \<< v
  204. be / IP 1 \->LIST + v
  205. be MOD 'v' STO
  206.               \>>
  207.             NEXT
  208.           END bt 3
  209. ==
  210.           THEN DROP
  211. v b TORATIOS RP
  212.           END 3
  213. DROPN v b
  214. "Invalid Base Type"
  215. DOERR
  216.         END
  217.       \>>
  218.     \>>
  219.   ADDVULIST
  220.     \<< \-> v1 v2 b
  221.       \<< v1 b BV v2
  222. b BV + b RP
  223.       \>>
  224.     \>>
  225.   SUBVULIST
  226.     \<< \-> v1 v2 b
  227.       \<< v1 b BV v2
  228. b BV - b RP
  229.       \>>
  230.     \>>
  231.   TOUNIT
  232.     \<< \-> n u
  233.       \<<
  234.         IF u TYPE '
  235. 1_m' TYPE ==
  236.         THEN n u
  237. \->UNIT
  238.         ELSE
  239.           IFERR n
  240. \->STR "_" + u \->STR
  241. DUP SIZE 1 - 2 SWAP
  242. SUB + OBJ\->
  243.           THEN
  244. # 514d DOERR
  245.           END
  246.         END
  247.       \>>
  248.     \>>
  249.   TORATIOS
  250.     \<< \-> u
  251.       \<< { 1 } 2 u
  252. SIZE
  253.         FOR j 1 u j
  254. 1 - GET TOUNIT 1 u
  255. j GET TOUNIT
  256. CONVERT UVAL 1
  257. \->LIST +
  258.         NEXT
  259.       \>>
  260.     \>>
  261.   VULTYPE
  262.     \<< DUP TYPE \-> b
  263. bt
  264.       \<<
  265.         IF bt 0
  266. TYPE ==
  267.         THEN 1
  268.         ELSE
  269.           IF bt { }
  270. TYPE ==
  271.           THEN
  272.             IF b 1
  273. GET TYPE 0 TYPE ==
  274.             THEN 2
  275.             ELSE 3
  276.             END
  277.           ELSE 4
  278.           END
  279.         END
  280.       \>>
  281.     \>>
  282.   VULVAL
  283.     \<< \-> b n
  284.       \<< b SIZE \-> bs
  285.         \<< 1
  286.           IF n bs <
  287.           THEN n 1
  288. + b SIZE
  289.             FOR j b
  290. j GET *
  291.             NEXT
  292.           END
  293.         \>>
  294.       \>>
  295.     \>>
  296. END
  297.  
  298. Craig A. Finseth            fin@unet.umn.edu [CAF13]
  299. University Networking Services        +1 612 624 3375 desk
  300. University of Minnesota            +1 612 626 1002 FAX
  301. 130 Lind Hall, 207 Church St SE, Minneapolis MN 55455-0134, U.S.A.
  302.  
  303.  
  304.